package net.ywd.xp1024search.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import net.ywd.xp1024search.config.CategoryProperties;
import net.ywd.xp1024search.service.XpSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Dreamingodd
 * @Date 2017/4/19.
 */
@RestController
@RequestMapping("/xpsearch")
public class XpSearchController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CategoryProperties categoryProperties;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private XpSearchService xpSearchService;

    @GetMapping("/")
    @ApiOperation(value = "自动搜索10页",
            notes = "参数实例：<br/>" +
            "日本騎兵<br/>" +
            "時間<br/>" +
            "1<br/>")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "categoryName", dataType = "String", required = true, value = "版面名称"),
            @ApiImplicitParam(paramType = "query", name = "searchText", dataType = "String", required = true, value = "搜索字段"),
            @ApiImplicitParam(paramType = "query", name = "startPageNum", dataType = "int", required = true, value = "开始页"),
    })
    public String search(@RequestParam String categoryName, @RequestParam String searchText, @RequestParam Integer startPageNum) throws Exception {
        long start = System.currentTimeMillis();
        List<Integer> pageNums = xpSearchService.search(categoryName, searchText, startPageNum);
        long end = System.currentTimeMillis();
        logger.info("Consumption: " + (end - start) / 1000.0 + "s");
        return objectMapper.writeValueAsString(pageNums);
    }
}
